﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BUS;
using DTO;

namespace GUI
{
    public partial class frmThemDonDatHangNVL : Form
    {
        public frmThemDonDatHangNVL()
        {
            InitializeComponent();
        }

        public List<NguyenVatLieu_SanPhamDTO> listNVL;
        public int manguyenvatlieu;

        private void btnThem_Click(object sender, EventArgs e)
        {
            try
            {
                int kq = -1;
                string themct = "";
                DonDH_NguyenVatLieuBUS ddhBUS = new DonDH_NguyenVatLieuBUS();
                DonDH_NguyenVatLieuDTO ddhDTO = new DonDH_NguyenVatLieuDTO();
                ddhDTO.NgayLap = DateTime.Parse(this.dtpNgayLap.Text);
                ddhDTO.DiaDiemGiaoHang = txtDiaDiemGiao.Text;
                ddhDTO.ThoiGianGiaohang = DateTime.Parse(this.dtpDate.Text);
                ddhDTO.MaNCC = int.Parse(this.cbMaNCC.SelectedValue.ToString());
                
                kq = ddhBUS.ThemDonDH_NguyenVatLieu(ddhDTO);

                //them chi tiet
                CT_DonDH_NguyenVatLieuBUS ctddhBUS = new CT_DonDH_NguyenVatLieuBUS();
                for (int i = 0; i < dgvChiTiet.Rows.Count; i++)
                {
                    try
                    {
                        CT_DonDH_NguyenVatLieuDTO ctddhDTO = new CT_DonDH_NguyenVatLieuDTO();
                        ctddhDTO.MaDDH = kq;
                        ctddhDTO.MaNVL = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[0].Value);
                        ctddhDTO.SoLuong = Convert.ToInt32(dgvChiTiet.Rows[i].Cells[2].Value);
                        if (ctddhDTO.MaNVL != -1)
                        {
                            themct = ctddhBUS.ThemChiTietDonDH_NguyenVatLieu(ctddhDTO);
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Thêm ct đơn đặt hàng không thành công!");
                    }
                }
                if (kq != -1 && themct != "")
                {
                    MessageBox.Show("Thêm đơn đặt hàng thành công!");
                }

            }
            catch (Exception ex)
            {
                 MessageBox.Show("Thêm đơn đặt hàng không thành công!" + ex.Message);
            }
        }

        private void buttonX4_Click(object sender, EventArgs e)
        {
            Close();
        }

        public void LoadComboboxNhaCungCap()
        {
            NhaCungCapBUS nccBUS = new NhaCungCapBUS();
            List<NhaCungCapDTO> listNCC = new List<NhaCungCapDTO>();
            listNCC = nccBUS.LayDanhSachNhaCungCap();
            this.cbMaNCC.DataSource = listNCC;
            this.cbMaNCC.DisplayMember = "MaNCC";
            cbMaNCC.ValueMember = "MaNCC";
            this.cbTenNCC.DataSource = listNCC;
            this.cbTenNCC.DisplayMember = "Ten";
            cbTenNCC.ValueMember = "MaNCC";
        }

        public void LoadComboboxNguyenVatLieu()
        {
            try
            {
                NguyenVatLieuBUS nvlBUS = new NguyenVatLieuBUS();
                listNVL = new List<NguyenVatLieu_SanPhamDTO>();
                listNVL = nvlBUS.LayDanhSachNguyenVatLieu();
                this.cbTenNVL.DataSource = listNVL;
                this.cbTenNVL.DisplayMember = "Ten";
                cbTenNVL.ValueMember = "MaNVL";
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi load combobox!" + ex.Message);
            }
        }
        private void frmThemDonDatHangNVL_Load(object sender, EventArgs e)
        {
            LoadComboboxNhaCungCap();
            LoadComboboxNguyenVatLieu();
        }

        private void dgvChiTiet_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dgvChiTiet.CurrentCell.ColumnIndex == 1)
            {
                ComboBox cbx = e.Control as ComboBox;
                if (cbx != null)
                {
                    cbx.SelectedIndexChanged -= comboBox_SelectedIndexChanged;
                    cbx.SelectedIndexChanged += comboBox_SelectedIndexChanged;
                }
            }
        }
        void comboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int manguyenvatlieu = Convert.ToInt32(((ComboBox)sender).SelectedValue);
                foreach (NguyenVatLieu_SanPhamDTO nvl in listNVL)
                {
                    if (nvl.MaNVL == manguyenvatlieu)
                    {
                        Double giaban = nvl.DonGiaBan;
                        string donvi = nvl.DonViTinh;
                        int manvl = nvl.MaNVL;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[0].Value = manvl.ToString();
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[3].Value = donvi;
                        dgvChiTiet.Rows[dgvChiTiet.CurrentCell.RowIndex].Cells[4].Value = giaban;

                    }
                }
            }
            catch (Exception)
            {

            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            int id = dgvChiTiet.CurrentCell.RowIndex;
            dgvChiTiet.Rows.RemoveAt(id);
        }

        private void dgvChiTiet_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0)
            {
                return;
            }
            int intIndex = e.RowIndex;
            dgvChiTiet.Rows[intIndex].Selected = true;
        }

        private void btnThemNCC_Click(object sender, EventArgs e)
        {
            frmThemNhaCungCap frm = new frmThemNhaCungCap();
            frm.ShowDialog();
        }
    }
}
